package com.mutual.system.appUser.controller;

import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.mutual.appUserInfo.pojo.AppUserInfo;
import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.util.SystemUtil;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.family.pojo.Family;
import com.mutual.family.service.FamilyService;

@Controller
@RequestMapping("/appUser")
public class AppUserController extends BaseController{

	@Resource
	private AppUserInfoService appUserInfoService;
	
	@Resource
	private FamilyService familyService;
	
	private static Logger logger = Logger.getLogger (AppUserController.class);
	
	/**
	 * 地市推广代理
	 * @return
	 */
	@RequestMapping(value = "/toCityPromotionCheckList")
	public String toCityPromotionCheckList(){
		return "system/appUser/cityPromotionCheckList";
	}
	
	@RequestMapping(value = "/toList")
	public String list(){
		return "system/appUser/appUserList";
	}
	
	@RequestMapping(value = "/listData")
	@ResponseBody
	public ResultList<AppUserInfo> listData(@RequestParam Map<String,Object> param){
		super.setAreaCode(param);
		total=appUserInfoService.selectAppUserInfoCount(param);
		ResultList<AppUserInfo> result=new ResultList<>();
		result.setCount(total);
		List<AppUserInfo> appUserList=appUserInfoService.selectAppUserInfoList(param);
		result.setData(appUserList);
		return result;
	}
	
	@RequestMapping(value = "/updateAppUserInfo")
	@ResponseBody
	public boolean updateAppUserInfo(AppUserInfo appUserInfo){
		boolean flag=false;
		try {
			//如果是推广人审核审核通过
			if(SystemUtil.getInt(appUserInfo.getCityPromotionCheckStatus())==AppUserInfo.CHECK_STATUS_PASS){
				Random random =new Random();
				String code = String.valueOf(random.nextInt(800000)+100000);
				appUserInfo.setInvitationCode(code);
			}
			appUserInfoService.updateByPrimaryKey(appUserInfo);
			flag=true;
		} catch (Exception e) {
			logger.error("updateAppUserInfo异常：", e);
		}
		return flag;
	}
	
	public static void main(String[] args) {
		for(int i=0;i<100;i++){
			String longInvitationCode=String.valueOf(System.currentTimeMillis()).substring(7);
			System.out.println(longInvitationCode);
		}
	}
	
	@RequestMapping(value = "/updateAppUserType")
	@ResponseBody
	public boolean updateAppUserInfo(String ids,int appUserType){
		boolean flag=false;
		try {
			String idArray[] = ids.split(",");
			Map<String,Object> param=Maps.newHashMap();
			param.put("ids", idArray);
			param.put("appUserType", appUserType);
			appUserInfoService.updateAppUserTypeBatch(param);
			flag=true;
		} catch (Exception e) {
			logger.error("updateAppUserInfo异常：", e);
		}
		return flag;
	}
	
	@RequestMapping(value = "/toAppUserDetail")
	public String toAppUserDetail(int id,Model model){
		AppUserInfo appUserInfo=appUserInfoService.selectByPrimaryKey(id);
		model.addAttribute("appUserInfo", appUserInfo);
		return "system/appUser/appUserDetail";
	}
	
	@RequestMapping(value = "/familyListData")
	@ResponseBody
	public ResultList<Family>  familyListData(Family family){
		List<Family> families=familyService.selectFamilyList(family);
		ResultList<Family> faResultList=new ResultList<>();
		faResultList.setData(families);
		return faResultList;
	}
	
	@RequestMapping(value = "/appUserProductListData")
	@ResponseBody
	public ResultList<AppUserInfoProduct>  appUserProductListData(AppUserInfoProduct a){
		List<AppUserInfoProduct> appUserInfoProducts=appUserInfoService.selectAppUserInfoProduct(a);
		ResultList<AppUserInfoProduct> faResultList=new ResultList<>();
		faResultList.setData(appUserInfoProducts);
		return faResultList;
	}
}
